﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;

namespace P2.Cuberry.Framework.CalendarLibrary
{
	public class DayColumnHeaderCell : DataGridViewColumnHeaderCell
	{
		protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
		{
			try
			{
				DayColumn dc = this.OwningColumn as DayColumn;
				DateTime now = DateTime.Now;
				
				if (now.CompareTo(dc.StartTime) >= 0 && now.CompareTo(dc.EndTime) <= 0)
				{
					LinearGradientBrush lgb = new LinearGradientBrush(cellBounds, SystemColors.Control, Color.Orange, LinearGradientMode.Vertical);
					graphics.FillRectangle(lgb, cellBounds);
				}
				else
				{
					graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);
				}

				graphics.DrawRectangle(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds);
				graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);

				StringFormat sf = new StringFormat();
				sf.Alignment = StringAlignment.Center;
				sf.LineAlignment = StringAlignment.Center;
				sf.Trimming = StringTrimming.EllipsisCharacter;

				RectangleF rect = new RectangleF(cellBounds.Left, cellBounds.Top, cellBounds.Width, cellBounds.Height);
				graphics.DrawString(this.Value.ToString(), new Font("Tahoma", 8), Brushes.Black, rect, sf);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
	}
}
